<?php
/**
 * Created by PhpStorm.
 * User: hoter.zhang
 * Date: 2016/8/15
 * Time: 11:53
 */

namespace app\core\components;


use yii\base\Theme;

class View extends \yii\web\View
{

    public function reder(){

    }
    /**
     * 添加設置主題的功能
     */
//    public $layout = null;
//    private  $sourcePath = null;


    public function init(){
        parent::init();
    }

    public function renderFile($viewFile, $params = [], $context = null) {
        if ($this->theme == null) {
            $this->setTheme();
        }
        //return $this->compress_html(parent::renderFile($viewFile, $params, $context));
        return parent::renderFile($viewFile, $params, $context);
    }

    public function setTheme(){
        $currentTheme = $this->getTheme();
        $config = [
            'pathMap' => [
                '@app/views' => '@webroot/template/'. $currentTheme . '/views' ,
                'baseUrl' => '@web/themes/basic',]
            /*'@app/views' => [
                '@statics/themes/' . $currentTheme . '/views',
                '@statics/themes/default/views'
            ],
            '@source/modules/' . $moduleId . '/home/views' => [
                '@statics/themes/' . $currentTheme . '/modules/' . $moduleId,
                '@statics/themes/default/modules/' . $moduleId
            ]
        ],
        'baseUrl' => '@statics/themes/default'*/

        ];
        $this->theme = new Theme($config);
    }

    public function getTheme() {
        //return $this->isMobile() ? "wap" : 'default';
        return 'default';
    }

    private function isMobile(){
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
            return true;
        //此条摘自TPM智能切换模板引擎，适合TPM开发
        if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
            return true;
        //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA']))
            //找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
        //判断手机发送的客户端标志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array(
                'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
            );
            //从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        //协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }

    public function setKeywords($arr=null){
        $basic_keywords = [];
        if (isset($arr)) {
            if (is_array($arr)) {
                $keywords = array_merge($arr, $basic_keywords);
            } else {
                $keywords = $basic_keywords;
                $keywords[] = $arr;
            }
        } else {
            $keywords = $basic_keywords;
        }
        $this->registerMetaTag([
            'name' => 'keywords',
            'content' => implode(',', $keywords),
        ]);
    }

    public function setGameDescription($str=''){
        $web_des = \Yii::$app->name.$str."專區,第一時間提供".$str."禮包、激活碼、并提供".$str."IOS版、安卓版下載,更有最新最熱的".$str."攻略分享,參與".\Yii::$app->name."論壇活動更有機會贏取大獎";
        $this->registerMetaTag([
            'name' => 'description',
            'content' => $web_des,
        ]);
    }

    public function setDescription($str){
        $this->registerMetaTag([
            'name' => 'description',
            'content' => $str
        ]);
    }

    private function compress_html($string) {
        $string = str_replace("\r\n", '', $string); //清除换行符
        $string = str_replace("\n", '', $string); //清除换行符
        $string = str_replace("\t", '', $string); //清除制表符
        $pattern = array (
            "/> *([^ ]*) *</", //去掉注释标记
            "/[\s]+/",//除掉空格
            "/<!--[^!]*-->/",
            "/\" /",
            "/ \"/",
            "'/\*[^*]*\*/'"
        );
        $replace = array (
            ">\\1<",
            " ",
            "",
            "\"",
            "\"",
            ""
        );
        return preg_replace($pattern, $replace, $string);
    }
}